<?php
require_once('configuracion.php');
/**
 * Description of BDManager
 *
 * @author Nelifer
 */
class BDManager {
    var $conect;  
    var $BaseDatos;
    var $Servidor;
    var $Usuario;
    var $Clave;
    function BDManager(){
        global $hostname,$username,$password, $dbname;
        $this->BaseDatos = $dbname;
        $this->Servidor = $hostname;
        $this->Usuario = $username;
        $this->Clave = $password;
    }

    public function conectar() {
        if(!($con=@mysql_connect($this->Servidor,$this->Usuario,$this->Clave))){
            echo"<h1> [:(] Error al conectar a la base de datos</h1>";      
            exit();
        }
        if (!@mysql_select_db($this->BaseDatos,$con)){
            echo "<h1> [:(] Error al seleccionar la base de datos</h1>";  
            exit();
        }
        $this->conect=$con;
        @mysql_query("SET NAMES 'utf8'");
        return true;    
    }
    public function _conectar(){
        if(!($con=@mysql_connect($this->Servidor,$this->Usuario,$this->Clave))){
            echo"<h1> [:(] Error al conectar a la base de datos</h1>";      
            exit();
        }
        if (!@mysql_select_db($this->BaseDatos,$con)){
            echo "<h1> [:(] Error al seleccionar la base de datos</h1>";  
            exit();
        }
        $this->conect=$con;
        @mysql_query("SET NAMES 'utf8'");
        return $this->conect;
    }
    public function fetch_array($consulta){ 
        return mysql_fetch_array($consulta);
    }
    public function num_rows($consulta){ 
        return mysql_num_rows($consulta);
    }
    public function getTotalConsultas(){
        return $this->total_consultas;
    }
    public function close($connect){
        mysql_close($connect);
    }
    public function get_current_insert_id($table){
        //$q = "SELECT LAST_INSERT_ID() FROM $table";
        
        $sql = "SELECT AUTO_INCREMENT AS ID FROM information_schema.TABLES ".
               "WHERE TABLE_SCHEMA = '$this->BaseDatos' ". 
               "AND TABLE_NAME = '$table'";
        $res=  mysql_query($sql);
        $id = 0;
        if($row = mysql_fetch_array( $res )){
            $id = $row["ID"];
        }
        return $id - 1;
    }
    
}

?>
